/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.business.handler.impl;

import java.util.Iterator;

import org.jb.coinche.client.business.message.AddUserMessage;
import org.jb.coinche.client.business.model.Game;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.ui.card.CardPanel;
import org.jb.coinche.client.ui.card.SetPanel;
import org.jb.coinche.client.ui.card.StatusPanel;
import org.jb.statemachine.client.GUIStateHandler;
import org.jb.statemachine.client.MessageSender;
import org.jb.user.client.UserLogin;

import com.google.inject.Inject;

/**
 * Handler lorsque le jeu n'a pas encore demarré (etat "Game waiting")
 */
public class GUIGameHandler implements GUIStateHandler<Game> {

	/*
	 * (non-Javadoc)
	 * @see org.jb.statemachine.client.GUIStateHandler#disable()
	 */
	@Override
	public void disable() {}

	/*
	 * (non-Javadoc)
	 * @see org.jb.statemachine.client.GUIStateHandler#enable()
	 */
	@Override
	public void enable() {}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#update(org.jb.coinche.client.guistatemachine.State)
	 */
	@Override
	public void update(Game state) {
		
		boolean playerIsPresent = state.getPlayer( user ) != null;
		
		statusPanel.setStatus("En attente de joueur...");
		
		//
		// On ajoute les users
		//
		Iterator<Player> it = state.getPlayers().iterator();
		int i = 0;
		for( CardPanel cp : setPanel.getCardPanels() ) {
			
			Player p = it.hasNext() ? it.next() : null;
			final int index = i++;
			
			if (p == null) {
				if (playerIsPresent) {
					cp.setParameters("", "En attente...");
				} else {
					cp.setParameters("", "Rejoindre", new CardPanel.ActionHandler() {
						/*
						 * (non-Javadoc)
						 * @see org.jb.coinche.client.ui.card.CardPanel.ActionHandler#execute()
						 */
						@Override
						public void execute() {				
							AddUserMessage msg = new AddUserMessage();
							msg.setIndex( index );
							sender.send( msg );
						}
					});
				}
			} else {
				cp.setParameters( p.getName(), "");
			}		
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<Game> getStateClass() {
		return Game.class;
	}

	@Inject
	private SetPanel setPanel;
	
	@Inject
	private MessageSender sender;
	
	@Inject
	private StatusPanel statusPanel;
	
	@Inject @UserLogin
	private String user;
}
